<template>
	<view class="column">
		<view class="header">
			<image src="/static/logo.png" />
		</view>
		<view class="row center"><view>请完成微信授权以继续使用</view></view>
		<button class="bottom" type="primary" @click="bindGetUserInfo" v-if="show">微信授权登录</button>
		<button class="bottom" type="primary" open-type="getPhoneNumber" lang="zh_CN" @getphonenumber="bindGetPhoneNumber" v-else>进入</button>
		<view class="row center">
			<u-icon name="/static/img/wcx.png" size="30"></u-icon>
			<view style="margin-left:10upx;font-size: 24upx;">已阅读并同意</view>
			<navigator url="/pages/public/webView" class="selym">《沂管家用户协议》</navigator>
			<navigator url="/pages/public/webView" class="selym">《隐私政策》</navigator>
		</view>
		<mix-loading v-if="isLoading"></mix-loading>
	</view>
</template>

<script>
export default {
	data() {
		return {
			openid: '',
			userInfo: {},
			show: true
		};
	},
	methods: {
		bindGetPhoneNumber(e) {
			console.log(e);
			let self = this;
			uni.getSetting({
				success(wxoptions) {
					if (wxoptions.authSetting['scope.userInfo']) {
						uni.login({
							success: async options => {
								console.log(options);
								let parmas = {
									encryptedData: e.detail.encryptedData,
									iv: e.detail.iv,
									openid: self.openid,
									code: options.code,
									nickname: self.userInfo.nickName,
									icon: self.userInfo.avatarUrl
								};
								const res = await self.$request('getPhoneNum', parmas, { showLoading: true });
								if (res.result == 0) {
									uni.setStorageSync('uid', res.uid);
									self.back();
								} else {
									self.$util.msg(res.resultNote);
								}
							}
						});
					}
				}
			});
		},
		bindGetUserInfo() {
			console.log(11111);
			let self = this;
			uni.getUserProfile({
				desc: '用于完善个人信息',
				success: e => {
					console.log(e);
					if (e.errMsg == 'getUserProfile:ok') {
						uni.login({
							success: async res => {
								console.log(res);
								self.userInfo = e.userInfo;
								let parmas = {
									code: res.code
								};
								const response = await self.$request('user/login-mini', parmas, { showLoading: true });
								console.log(response);
								if (response.result == 0) {
									// : "0首次登录 1老用户",
									if (response.isnewuser == 0) {
										self.show = false;
										self.openid = response.openid;
									} else {
										uni.setStorageSync('uid', response.uid);
										self.back();
									}
								} else {
									self.$util.msg(response.resultNote);
								}
							}
						});
					}
				}
			});
		}
	}
};
</script>
<style>
page {
	background: #ffffff;
}
</style>
<style scoped lang="less">
.selym {
	color: #4e9fc1;
	font-size: 24upx;
}
.header {
	margin: 60px auto;
	text-align: center;
}

.hide {
	opacity: 0;
}

.header image {
	width: 278upx;
	height: 80upx;
}

.bottom {
	height: 80upx;
	width: 400upx;
	background: #4e9fc1 !important;
	border-radius: 12upx;
	margin: 70upx auto;
	font-size: 30upx;
	color: #ffffff;
}
</style>
